<template>
  <div class="app-container clue1-page">
    <!-- <el-button type="danger">危险按钮</el-button> -->
    <clue-step :current="1"></clue-step>
    <!-- 搜索表单区域 -->
    <el-form
      :inline="true"
      :model="searchForm"
      class="form-search"
      size="small"
    >
      <el-form-item label="线索号">
        <el-input v-model="searchForm.id" placeholder="线索号"></el-input>
      </el-form-item>
      <el-form-item label="线索名称">
        <el-input
          v-model="searchForm.clueName"
          placeholder="线索名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input
          v-model="searchForm.relativeName"
          placeholder="联系人"
        ></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input
          v-model="searchForm.relativePhone"
          placeholder="联系电话"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-search"
          @click="getAllClue"
        >
          查询
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetSearch">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮区域 -->
    <div class="btn-box">
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="dialogAddVisible = true"
      >
        新增线索
      </el-button>
      <div>
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button
            size="mini"
            circle
            icon="el-icon-refresh"
            :disabled="loading"
            @click="getAllClue"
          ></el-button>
        </el-tooltip>
      </div>
    </div>
    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="tableData" stripe style="width: 100%">
      <!-- 表格折叠区域 -->
      <el-table-column fixed type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="线索ID">
              <span>{{ scope.row.id }}</span>
            </el-form-item>
            <el-form-item label="线索名称">
              <span>{{ scope.row.clueName }}</span>
            </el-form-item>
            <el-form-item label="征集方式">
              <span>{{ scope.row.way }}</span>
            </el-form-item>
            <el-form-item label="线索来源">
              <span>{{ scope.row.sourceCome }}</span>
            </el-form-item>
            <el-form-item label="分类">
              <span>{{ scope.row.sortName }}</span>
            </el-form-item>
            <el-form-item label="年代">
              <span>{{ scope.row.yearName }}</span>
            </el-form-item>
            <el-form-item label="等级">
              <span>{{ scope.row.gradeName }}</span>
            </el-form-item>
            <el-form-item label="发现日期">
              <span>{{ scope.row.findTime }}</span>
            </el-form-item>
            <el-form-item label="报价">
              <span>{{ scope.row.price }} </span>
            </el-form-item>
            <el-form-item label="征集人">
              <span>{{ scope.row.userName }}</span>
            </el-form-item>
            <el-form-item label="联系人">
              <span>{{ scope.row.relativeName }}</span>
            </el-form-item>
            <el-form-item label="身份证号">
              <span>{{ scope.row.userIdCard }}</span>
            </el-form-item>
            <el-form-item label="联系电话">
              <span>{{ scope.row.relativePhone }}</span>
            </el-form-item>
            <el-form-item label="鉴选描述">
              <span>{{ scope.row.identifyDesc }}</span>
            </el-form-item>
            <el-form-item label="线索描述">
              <span>{{ scope.row.clueDesc }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column fixed prop="id" label="线索号"></el-table-column>
      <el-table-column prop="clueName" label="线索名称"></el-table-column>
      <el-table-column prop="sourceCome" label="线索来源"></el-table-column>
      <el-table-column
        width="110"
        prop="sortName"
        label="藏品分类"
      ></el-table-column>
      <el-table-column prop="userName" label="征集人"></el-table-column>
      <el-table-column
        prop="relativePhone"
        width="110"
        label="联系电话"
      ></el-table-column>
      <el-table-column prop="price" label="报价"></el-table-column>
      <el-table-column prop="way" label="征集方式"></el-table-column>
      <el-table-column
        prop="findTime"
        width="110"
        label="发现日期"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        width="150"
        label="征集时间"
      ></el-table-column>
      <!--  -->
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <!-- <el-button
            type="primary"
            plain
            size="small"
            @click="commitClue(scope.row.id)"
          >
            征集对象
          </el-button> -->
          <el-button
            type="danger"
            plain
            size="small"
            @click="deleteClue(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <div class="pagination-warp">
      <el-pagination
        :current-page="current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <ClueAddDialog></ClueAddDialog>
  </div>
</template>

<script>
import ClueStep from '../components/ClueStep.vue';
import {
  getAllClue,
  getYearAndGradeAndSort,
  deleteClue,
  commitClue
} from '@/api/clue/clue-1';
import ClueAddDialog from './clue-add';
export default {
  components: { ClueStep, ClueAddDialog },
  data() {
    return {
      loading: true, // 加载遮罩
      dialogAddVisible: false,
      tableData: [], // 表格数据
      current: 1, // 当前页
      limit: 10, // 一页几个
      total: 0, // 总数
      searchForm: {
        id: '',
        clueName: '',
        userName: '',
        relativePhone: ''
      },
      // 等级
      gradeList: [],
      // 分类
      sortList: [],
      // 年代
      yearList: [],
      // 审批批语
      commitDesc: ''
    };
  },
  created() {
    this.getAllClue();
    this.getYearAndGradeAndSort();
  },
  methods: {
    /**
     * 线索详情对话框
     */
    showDetailDialog() {
      this.dialogVisible = true;
    },
    /**
     * 处理分页修改
     */
    handleSizeChange(val) {
      this.limit = val;
      this.getAllClue();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getAllClue();
    },
    /**
     * 获取全部线索
     */
    async getAllClue() {
      this.loading = true;
      const { data: res } = await getAllClue(
        this.current,
        this.limit,
        this.searchForm
      );
      console.log('api=获取全部线索', res);
      this.tableData = res.data.ClueList;
      this.total = res.data.total;
      this.loading = false;
    },
    /**
     * 重置查询
     */
    resetSearch() {
      this.searchForm = {
        roleId: null,
        userName: '',
        userPhone: ''
      };
      this.getAllClue();
    },
    /**
     * 获取年份，等级，分类
     */
    async getYearAndGradeAndSort() {
      const { data: res } = await getYearAndGradeAndSort();
      console.log('api=获取年份，等级，分类', res);
      this.sortList = res.data.sortList;
      this.gradeList = res.data.gradeList;
      this.yearList = res.data.yearList;
    },
    /**
     * 提交征集线索
     */
    commitClue(id) {
      this.$prompt('请输入建议', '征集对象', {
        distinguishCancelAndClose: true,
        confirmButtonText: '同意',
        cancelButtonText: '拒绝',
        cancelButtonClass: 'el-button--danger',
        inputValidator: val => {
          this.commitDesc = val;
          return true;
        }
      })
        .then(async ({ value }) => {
          const { data: res } = await commitClue({
            id: id,
            desc: value,
            status: 1
          });
          console.log('api=审批线索', res);
          if (res.code === 200) {
            this.getAllClue();
            this.$message({
              type: 'success',
              message: res.message
            });
          }
        })
        .catch(async (action, instance) => {
          if (action !== 'cancel') return;
          console.log(action, instance);
          const { data: res } = await commitClue({
            id: id,
            desc: this.commitDesc,
            status: -1
          });
          console.log('api=审批线索', res);
          if (res.code === 200) {
            this.getAllClue();
            this.$message({
              type: 'success',
              message: res.message
            });
          }
        });
    },
    /**
     * 删除线索
     * @param id 线索id
     */
    async deleteClue(id) {
      this.$confirm('此操作将永久删除该线索, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await deleteClue(id);
          console.log('api=删除线索', res);
          if (res.code === 200) {
            // if (this.userStatus)
            this.$message.success('删除成功!');
            this.getAllClue();
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {});
    }
  }
};
</script>

<style lang="scss">
.form-search {
  .el-input {
    width: 180px;
  }
}
</style>

<style lang="scss" scoped>
.clue1-page {
  .pagination-warp {
    margin-top: 20px;
    position: relative;
    .el-pagination {
      position: absolute;
      right: 0;
      top: 0;
    }
  }

  .el-table {
    margin-top: 10px;
  }

  .btn-box {
    display: flex;
    justify-content: space-between;
  }

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
}
</style>
